package com.koron.order.linepatrol.uav.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.StringUtils;
import com.koron.order.linedefect.base.bean.po.DefectBean;
import com.koron.order.linedefect.base.bean.query.DefectQuery;
import com.koron.order.linedefect.base.service.DefectService;
import com.koron.order.linepatrol.check.bean.po.LinePatrolCheckSatellite;
import com.koron.order.linepatrol.check.bean.query.LinePatrolCheckSatelliteQuery;
import com.koron.order.linepatrol.check.bean.vo.LinePatrolCheckSatelliteVO;
import com.koron.order.linepatrol.check.service.LinePatrolCheckSatelliteService;
import com.koron.order.linepatrol.uav.bean.VO.LineRiskPatrolUnvDetail;
import com.koron.order.linepatrol.uav.bean.dto.LineRiskPatrolUnvDetailDTO;
import com.koron.order.linepatrol.uav.bean.query.LineRiskPatrolUnvDetailQuery;
import com.koron.order.linepatrol.uav.mapper.LineRiskPatrolUnvDetailMapper;
import com.koron.order.linepatrol.uav.service.LineRiskPatrolUnvDetailService;
import com.koron.order.overhaul.bean.dto.Equipment;
import com.koron.order.risk.base.bean.query.LineRiskQuery;
import com.koron.order.risk.base.bean.vo.LineRiskVO;
import com.koron.order.risk.base.mapper.LineEquipmentMapper;
import com.koron.order.risk.base.service.LineRiskService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class LineRiskPatrolUnvDetailServiceImpl implements LineRiskPatrolUnvDetailService {

    @Autowired
    LinePatrolCheckSatelliteService linePatrolCheckSatelliteService;

    @Autowired
    LineRiskService lineRiskService;

    @Autowired
    DefectService defectService;

    public EamUser getCurr() {
        return ThreadLocalContext.get();
    }

    public String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineRiskPatrolUnvDetail> page(SessionFactory factory, PageQuery pageQuery, LineRiskPatrolUnvDetailQuery query) {
        return LineRiskPatrolUnvDetailService.super.page(factory, pageQuery, query);
    }

    @Override
    @TaskAnnotation("getById")
    public LineRiskPatrolUnvDetail getById(SessionFactory factory, String id) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail = mapper.selectById(id);
        return lineRiskPatrolUnvDetail;
    }
    @Override
    @TaskAnnotation("selectDetailById")
    public LineRiskPatrolUnvDetailDTO selectDetailById(SessionFactory factory, String id) {
        LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail= getById(factory, id);
        LineRiskPatrolUnvDetailDTO lineRiskPatrolUnvDetailDTO = new LineRiskPatrolUnvDetailDTO();
        lineRiskPatrolUnvDetailDTO.setLineRiskPatrolUnvDetail(lineRiskPatrolUnvDetail);
        if(StringUtils.isNotEmpty(lineRiskPatrolUnvDetail.getCheckId())){
            LinePatrolCheckSatelliteQuery linePatrolCheckSatelliteQuery = new LinePatrolCheckSatelliteQuery();
            linePatrolCheckSatelliteQuery.setId(lineRiskPatrolUnvDetail.getCheckId());
            List<LinePatrolCheckSatellite> list = linePatrolCheckSatelliteService.list(factory, linePatrolCheckSatelliteQuery);
            LinePatrolCheckSatellite checkSatellite = list.stream().findFirst().orElse(new LinePatrolCheckSatellite());
            lineRiskPatrolUnvDetailDTO.setLinePatrolCheckSatellite(checkSatellite);
        }
        LineRiskQuery lineRiskQuery = new LineRiskQuery();
        lineRiskQuery.setPatrolId(lineRiskPatrolUnvDetail.getId());
        List<LineRiskVO> lineRiskVOS = lineRiskService.list(factory, lineRiskQuery);
        lineRiskPatrolUnvDetailDTO.setLineRiskOrders(lineRiskVOS);
        DefectQuery defectQuery = new DefectQuery();
        defectQuery.setPatrolId(lineRiskPatrolUnvDetail.getId());
        List<DefectBean> defectBeans = defectService.queryList(factory, defectQuery);
        lineRiskPatrolUnvDetailDTO.setDefectOrders(defectBeans);
        return lineRiskPatrolUnvDetailDTO;
    }
    @Override
    @TaskAnnotation("list")
    public List<LineRiskPatrolUnvDetail> list(SessionFactory factory, LineRiskPatrolUnvDetailQuery query) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("listByCheckId")
    public List<LineRiskPatrolUnvDetail> listByCheckId(SessionFactory factory, String checkId) {
        LineRiskPatrolUnvDetailQuery lineRiskPatrolUnvDetailQuery = new LineRiskPatrolUnvDetailQuery();
        lineRiskPatrolUnvDetailQuery.setCheckId(checkId);
        return list(factory, lineRiskPatrolUnvDetailQuery);
    }

    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail) {
        if (lineRiskPatrolUnvDetail.getCheckStatus() == null) {
            lineRiskPatrolUnvDetail.setCheckStatus("0");
        }
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.insertOrUpdate(lineRiskPatrolUnvDetail) > 0;

    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<LineRiskPatrolUnvDetail> lineRiskPatrolUnvDetailList) {
        if (CollUtil.isEmpty(lineRiskPatrolUnvDetailList)) {
            return false;
        }
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.insertOrUpdateBatch(lineRiskPatrolUnvDetailList) > 0;

    }

    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        int update = mapper.update(lineRiskPatrolUnvDetail);
        return update > 0;
    }


    @Override
    public boolean fillCheck(SessionFactory factory, LinePatrolCheckSatelliteVO linePatrolCheckSatelliteVO, List<String> patrolIds) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.fillCheck(linePatrolCheckSatelliteVO, patrolIds) > 0;
    }

    @Override
    public boolean clearCheck(SessionFactory factory, String checkId) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.clearCheck(checkId) > 0;
    }

    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineRiskPatrolUnvDetailMapper mapper = factory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        return mapper.hidden(id) > 0;
    }

    @TaskAnnotation("equipment")
    public List<Equipment> allLineEquipment() {
        // 1. 查询根设备
        Map<String, String> config = ConfigInfoTool.getConfigByCode("equipment", "line_root_device");
        String id = config.get("id");
        try (SessionFactory factory = new SessionFactory()) {
            factory.setDbEnv(getCurrDs());
            LineEquipmentMapper mapper = factory.getMapper(LineEquipmentMapper.class);
            Equipment root = mapper.getById(id);
            List<Equipment> equipmentList = mapper.selectSimpleLineEquipment(root.getLevelPath(), root.getLevel() + 3);
            // 线路设备
            Map<String, Equipment> lineEquipmentMap = new HashMap<>();
            for (Equipment equipment : equipmentList) {
                // 本身equipmentList就是有序的
                // 添加线路设备
                String parentId = equipment.getParentId();
                if (parentId.equals(root.getId())) {
                    if (root.getChildren() == null) {
                        root.setChildren(new ArrayList<>(Collections.singletonList(equipment)));
                    } else {
                        List<Equipment> children = root.getChildren();
                        children.add(equipment);
                    }
                    lineEquipmentMap.put(equipment.getId(), equipment);
                }

                // 添加杆塔
                if (lineEquipmentMap.containsKey(parentId)) {
                    Equipment parent = lineEquipmentMap.get(parentId);
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>(Collections.singletonList(equipment)));
                    } else {
                        List<Equipment> children = parent.getChildren();
                        children.add(equipment);
                    }
                }
            }
            return root.getChildren();
        }
    }

}

